package com.naiterui.ehp.bs.patient.repository;

import com.naiterui.common.repository.core.BaseJpaRepository;
import com.naiterui.ehp.bp.domain.PatientPersonDoctor;
import com.naiterui.ehp.bp.domain.PatientPersonDoctorPK;
import java.util.Collection;
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

/**
 * <pre>
 *
 * </pre>
 *
 * @author fangguanhong
 * @date 2020/4/10 下午5:30
 * @since 1.0.0
 */
@Repository
public interface IApPatientPersonDoctorRepository extends BaseJpaRepository<PatientPersonDoctor, PatientPersonDoctorPK> {

    List<PatientPersonDoctor> findByPersonPKPatientIdOrderByCreateAtAsc(Long patientId);

    List<PatientPersonDoctor> findByPersonPKPatientIdAndPersonPKDoctorIdAndRelationFlagOrderByCreateAtAsc(Long patientId, Long doctorId, Integer relationFlag);

    List<PatientPersonDoctor> findByPersonPKPatientIdAndPersonPKDoctorIdOrderByCreateAtAsc(Long patientId, Long doctorId);

    PatientPersonDoctor findFirstByPersonPKPatientIdAndPersonPKDoctorId(Long patientId, Long doctorId);

    Integer countByPersonPKPatientIdAndPersonPKDoctorId(Long patientId, Long doctorId);

    @Query(nativeQuery = true, value = "select doctor_id,patient_id from patient_personal_doctor where patient_id = ?1 and doctor_id in(?2)")
    List<Object[]> existPersonalByDoctorIds(Long patientId, String queryStr);

    List<PatientPersonDoctor> findByPersonPKPatientIdAndPersonPKDoctorIdIn(Long patientId, List<Long> doctorId);
    Integer countByPersonPKPatientIdAndPersonPKDoctorIdIn(Long patientId, List<Long> doctorId);

    List<PatientPersonDoctor> findByPersonPKPatientIdOrderByCreateAtDesc(Long patientId);

    //@Query("insert into PatientPersonDoctor(personPK.patientId, personPK.doctorId, source) values(?1, ?2, ?3)")
    //void saveOne(Long patientId, Long doctorId, Integer source);

    List<PatientPersonDoctor> findByPersonPKPatientIdAndPersonPKDoctorIdIn(Long patientId, Collection<Long> doctorIds);



}
